package cn.com.ghy.android.widget.visit;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.ghy.android.bean.CompeteBrandInfoBean;
import cn.com.ghy.android.bean.CustomerInfoBean;
import cn.com.ghy.android.bean.DataCollVisitComboxBean;
import cn.com.ghy.android.bean.DataCollectBaseInfoBean;
import cn.com.ghy.android.bean.DataCollectCompeteInfoBean;
import cn.com.ghy.android.bean.DataCollectDayInfoBean;
import cn.com.ghy.android.bean.DataCollectInfoBean;
import cn.com.ghy.android.bean.DataCollectSaleInfoBean;
import cn.com.ghy.android.bean.DisplaySiteBean;
import cn.com.ghy.android.bean.GoodsTypeBean;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.bean.PictureTypeBean;
import cn.com.ghy.android.bean.SaleTypeBean;
import cn.com.ghy.android.bean.UnitInfoBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.ImageService;
import cn.com.ghy.android.service.VisitOpeService;
import cn.com.ghy.android.style.MenuTabContent;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;

public class DataCollect extends TabActivity {

	private int totleHeight = 0;
	private int excelViewHeight = 0;
	private Intent intent = null;
	private Bundle bundle = null;
	private ProgressDialog progressDialog = null;

	private TabHost tabHost = null;
	private ImageButton btnList = null;
	private ImageButton btnBack = null;

	private MenuTabContent.MenuBodyAdapter bodyAdapter = null;
	private MenuTabContent tabMenu;
	private View appToolBar = null;

	private String[] taskStateStrArr = null;
	private LoginInfoBean loginInfo = null;
	private CustomerInfoBean customerInfoForList = null;
	private int todayVisitPosition = -1;
	private DataCollectInfoBean dataCollectInfo = null;
	private List<DataCollVisitComboxBean> dataCollVisitComboxList = null;
	private List<DataCollectBaseInfoBean> dataCollectBaseInfoList = null;
	private List<DataCollectDayInfoBean> dataCollectDayInfoList = null;
	private List<DataCollectSaleInfoBean> dataCollectSaleInfoList = null;
	private List<DataCollectCompeteInfoBean> dataCollectCompeteInfoList = null;

	private List<UnitInfoBean> unitInfoList = null;
	private List<CompeteBrandInfoBean> competeBrandInfoList = null;
	private List<SaleTypeBean> saleTypeList = null;
	private List<DisplaySiteBean> displaySiteList = null;
	private List<PictureTypeBean> pictureTypeList = null;
	private List<GoodsTypeBean> goodsTypeList = null;

	private String resultStatus = "ERROR";
	private HttpThread httpThread = null;

	private DataCollVisitComboxBean dataCollVisitCombox = null;

	private TextView labTxtCustomerId = null;
	private TextView labTxtCustomerName = null;
	private EditText txtDisplayScale = null;
	private Spinner spiDisplaySite = null;
	private EditText txtVisitNote = null;
	private EditText txtQuesPlan = null;
	private RadioGroup rgpIsVisit = null;
	private RadioButton radIsYes = null;
	private RadioButton radIsNo = null;
	private LinearLayout layIsJxOrFx = null;
	private CheckBox chkIsPaycust = null;

	private LinearLayout linDataCollDayList = null;
	private DataCollDayListView dataCollDayListView = null;
	private ListView dataCollDayClickView = null;
	private TextView labTextDataCollDaySkuNum = null;
	private DataCollectBaseInfoBean dataCollectBaseInfo = null;
	private cn.com.ghy.android.widget.visit.DataCollDayListView.ExcelAdapter dataCollDayAdapter = null;

	private LinearLayout linDataCollSaleList = null;
	private DataCollSaleListView dataCollSaleListView = null;
	private ListView dataCollSaleClickView = null;
	private TextView labTextDataCollSaleSkuNum = null;
	private cn.com.ghy.android.widget.visit.DataCollSaleListView.ExcelAdapter dataCollSaleAdapter = null;

	private LinearLayout linDataCollCompeteList = null;
	private DataCollCompeteListView dataCollCompeteListView = null;
	private ListView dataCollCompeteClickView = null;
	private TextView labTextDataCollCompeteSkuNum = null;
	private cn.com.ghy.android.widget.visit.DataCollCompeteListView.ExcelAdapter dataCollCompeteAdapter = null;

	private ImageView imgPhoto = null;
	private Spinner spiPictureType = null;
	private EditText txtPictureCommont = null;
	private Button btnOnPhoto = null;
	private Button btnUpPhoto = null;
	private byte[] imageData = null;
	private String picType = null;
	private String picName = null;
	private String picCommont = null;

	private DisposeException disposeException = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSession();
		setContentView(R.layout.data_collect);
		setToolBar();

		tabHost = this.getTabHost();
		tabHost = this.getTabHost();
		TabWidget tabWidget = tabHost.getTabWidget();
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("基本")
				.setContent(R.id.layDataCollectBase));
		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("日常")
				.setContent(R.id.layDataCollectDay));
		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("促销")
				.setContent(R.id.layDataCollectSale));
		tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("竞品")
				.setContent(R.id.layDataCollectCompete));
		tabHost.addTab(tabHost.newTabSpec("tab5").setIndicator("图片")
				.setContent(R.id.layDataCollectImage));
		setMenu(tabHost.getCurrentTabTag());
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			tabWidget.getChildAt(i).getLayoutParams().height = this.getWindowManager()
					.getDefaultDisplay().getHeight() / 16;
		}

		findView();
		init();
		addListener();
	}

	private void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		loginInfo = ((ComStaticApp) this.getApplication()).getLoginInfo();
		bundle = this.getIntent().getExtras();
		taskStateStrArr = bundle.getStringArray("taskStateStrArr");
		customerInfoForList = (CustomerInfoBean) bundle.getSerializable("customerInfo");
		todayVisitPosition = bundle.getInt("position");
		dataCollectInfo = (DataCollectInfoBean) bundle.getSerializable("dataCollectInfo");
		dataCollVisitComboxList = dataCollectInfo.getDataCollVisitComboxList();
		dataCollectBaseInfoList = dataCollectInfo.getDataCollectBaseInfoList();
		dataCollectDayInfoList = dataCollectInfo.getDataCollectDayInfoList();
		dataCollectSaleInfoList = dataCollectInfo.getDataCollectSaleInfoList();
		dataCollectCompeteInfoList = dataCollectInfo.getDataCollectCompeteInfoList();

		dataCollVisitCombox = dataCollVisitComboxList.get(0);
		unitInfoList = dataCollVisitCombox.getUnitInfoList();
		competeBrandInfoList = dataCollVisitCombox.getCompeteBrandInfoList();
		saleTypeList = dataCollVisitCombox.getSaleTypeList();
		displaySiteList = dataCollVisitCombox.getDisplaySiteList();
		displaySiteList.add(0, new DisplaySiteBean("-1", "请选择"));
		pictureTypeList = dataCollVisitCombox.getPictureTypeList();
		goodsTypeList = dataCollVisitCombox.getGoodsType();

		dataCollectBaseInfo = dataCollectBaseInfoList.get(0);
	}

	private void findView() {

		totleHeight = this.getWindowManager().getDefaultDisplay().getHeight();
		excelViewHeight = (int) (totleHeight / 1.61);

		labTxtCustomerId = (TextView) this.findViewById(R.id.labTxtCustomerId);
		labTxtCustomerName = (TextView) this.findViewById(R.id.labTxtCustomerName);
		rgpIsVisit = (RadioGroup) this.findViewById(R.id.rgpIsVisit);
		radIsYes = (RadioButton) this.findViewById(R.id.radIsYes);
		radIsNo = (RadioButton) this.findViewById(R.id.radIsNo);
		;
		txtDisplayScale = (EditText) this.findViewById(R.id.txtDisplayScale);
		spiDisplaySite = (Spinner) this.findViewById(R.id.spiDisplaySite);
		txtVisitNote = (EditText) this.findViewById(R.id.txtVisitNote);
		layIsJxOrFx = (LinearLayout) this.findViewById(R.id.layIsJxOrFx);
		txtQuesPlan = (EditText) this.findViewById(R.id.txtQuesPlan);
		chkIsPaycust = (CheckBox) findViewById(R.id.chkIsPaycust);

		setExcelTitle();

		labTextDataCollDaySkuNum = (TextView) this.findViewById(R.id.labTextDataCollDaySkuNum);
		linDataCollDayList = (LinearLayout) this.findViewById(R.id.linDataCollDayList);
		linDataCollDayList.addView(dataCollDayListView.getView());
		dataCollDayClickView = dataCollDayListView.getListView();

		labTextDataCollSaleSkuNum = (TextView) this.findViewById(R.id.labTextDataCollSaleSkuNum);
		linDataCollSaleList = (LinearLayout) this.findViewById(R.id.linDataCollSaleList);
		linDataCollSaleList.addView(dataCollSaleListView.getView());
		dataCollSaleClickView = dataCollSaleListView.getListView();

		labTextDataCollCompeteSkuNum = (TextView) this
				.findViewById(R.id.labTextDataCollCompeteSkuNum);
		linDataCollCompeteList = (LinearLayout) this.findViewById(R.id.linDataCollCompeteList);
		linDataCollCompeteList.addView(dataCollCompeteListView.getView());
		dataCollCompeteClickView = dataCollCompeteListView.getListView();

		imgPhoto = (ImageView) this.findViewById(R.id.imgPhoto);
		spiPictureType = (Spinner) this.findViewById(R.id.spiPictureType);
		txtPictureCommont = (EditText) this.findViewById(R.id.txtPictureCommont);
		btnOnPhoto = (Button) this.findViewById(R.id.btnOnPhoto);
		btnUpPhoto = (Button) this.findViewById(R.id.btnUpPhoto);
	}

	private void setToolBar() {
		appToolBar = findViewById(R.id.appToolBar);
		btnList = (ImageButton) appToolBar.findViewById(R.id.btnList);
		btnBack = (ImageButton) appToolBar.findViewById(R.id.btnBack);
	}

	private void init() {
		labTxtCustomerId.setText(customerInfoForList.getPartner());
		labTxtCustomerName.setText(customerInfoForList.getName_org1());
		if ("Y".equals(dataCollectBaseInfo.getCustomerInfo().getIsvisit())) {
			radIsYes.setChecked(true);
		} else {
			radIsNo.setChecked(true);
		}

		if ("Y".equals(dataCollectBaseInfo.getIsPaycust())) {
			chkIsPaycust.setChecked(true);
		}

		LinearLayout.LayoutParams layoutParamsLab = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		layoutParamsLab.setMargins(0, 0, 5, 0);
		TextView tvLab = new TextView(DataCollect.this);
		tvLab.setTextSize(15);

		LinearLayout.LayoutParams layoutParamsTxt = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		layoutParamsTxt.setMargins(0, 0, 0, 0);
		TextView tvTxt = new TextView(DataCollect.this);
		tvTxt.setTextSize(15);

		if (null != dataCollectBaseInfo.getCustomerInfo().getJxsid()
				&& null != dataCollectBaseInfo.getCustomerInfo().getJxsname()) {
			tvLab.setText("经销商:");
			tvTxt.setText(dataCollectBaseInfo.getCustomerInfo().getJxsname());
		} else if (null != dataCollectBaseInfo.getCustomerInfo().getFxsid()
				&& null != dataCollectBaseInfo.getCustomerInfo().getFxsname()) {
			tvLab.setText("分销商:");
			tvTxt.setText(dataCollectBaseInfo.getCustomerInfo().getFxsname());
		}
		layIsJxOrFx.addView(tvLab, layoutParamsLab);
		layIsJxOrFx.addView(tvTxt, layoutParamsTxt);

		ArrayAdapter<DisplaySiteBean> displaySiteAdapter = new ArrayAdapter<DisplaySiteBean>(
				DataCollect.this, android.R.layout.simple_spinner_item, displaySiteList);
		displaySiteAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spiDisplaySite.setAdapter(displaySiteAdapter);
		if (null != dataCollectBaseInfo.getDisplaySite().getClwz()) {
			spiDisplaySite.setSelection(
					getSpinnerSelectedItemPosition(dataCollectBaseInfo.getDisplaySite()), true);
		}
		txtDisplayScale.setText(null == dataCollectBaseInfo.getDisplayScale() ? ""
				: dataCollectBaseInfo.getDisplayScale());
		txtVisitNote.setText(null == dataCollectBaseInfo.getVisitNote() ? "" : dataCollectBaseInfo
				.getVisitNote());
		txtQuesPlan.setText(null == dataCollectBaseInfo.getQuesPlan() ? "" : dataCollectBaseInfo
				.getQuesPlan());

		dataCollDayAdapter = dataCollDayListView.createAdapter(dataCollectDayInfoList);
		labTextDataCollDaySkuNum.setText(dataCollDayAdapter.getCount() + "");

		dataCollSaleAdapter = dataCollSaleListView.createAdapter(dataCollectSaleInfoList);
		labTextDataCollSaleSkuNum.setText(dataCollSaleAdapter.getCount() + "");
		dataCollCompeteAdapter = dataCollCompeteListView.createAdapter(dataCollectCompeteInfoList);
		labTextDataCollCompeteSkuNum.setText(dataCollCompeteAdapter.getCount() + "");

		ArrayAdapter<PictureTypeBean> picTypeAdapter = new ArrayAdapter<PictureTypeBean>(
				DataCollect.this, android.R.layout.simple_spinner_item, pictureTypeList);
		picTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spiPictureType.setAdapter(picTypeAdapter);

	}

	private void setExcelTitle() {
		int[] dataColDaycolWidth = new int[] { 50, 130, 80, 80, 80, 150 };
		String[] dataColDaycolName = new String[] { "序号", "SKU", "零售价", "单位", "排面数", "备注" };
		dataCollDayListView = new DataCollDayListView(DataCollect.this, 40, dataColDaycolName,
				dataColDaycolWidth, excelViewHeight);
		dataCollDayListView.setTitle();

		int[] dataColSalecolWidth = new int[] { 50, 130, 100, 100, 60, 110, 110, 140, 80, 80, 150,
				110 };
		String[] dataColSalecolName = new String[] { "序号", "SKU", "申请价格", "执行价格", "单位", "开始时间",
				"结束时间", "促销类型", "是否TG", "价格牌", "促销效果反馈", "填写日期" };
		dataCollSaleListView = new DataCollSaleListView(DataCollect.this, 40, dataColSalecolName,
				dataColSalecolWidth, excelViewHeight);
		dataCollSaleListView.setTitle();

		int[] dataColCompetecolWidth = new int[] { 50, 80, 80, 130, 100, 130, 130, 80, 80, 60, 110,
				110, 150 };
		String[] dataColCompetecolName = new String[] { "序号", "竞品", "品类", "上架SKU数", "陈列比例",
				"竞品SKU", "本公司SKU", "零售价", "促销价", "单位", "开始时间", "结束时间", "记录" };
		dataCollCompeteListView = new DataCollCompeteListView(DataCollect.this, 40,
				dataColCompetecolName, dataColCompetecolWidth, excelViewHeight);
		dataCollCompeteListView.setTitle();
	}

	private boolean getBaseInfo() {
		String displayScale = txtDisplayScale.getText().toString().trim();
		if (radIsYes.isChecked()) {
			if (null == displayScale || "".equals(displayScale)
					|| Double.parseDouble(displayScale) == 0) {
				Toast.makeText(DataCollect.this, "基本信息的陈列占比不能为0或者空!", Toast.LENGTH_SHORT).show();
				return false;
			}
			if ("-1".equals(dataCollectBaseInfo.getDisplaySite().getClwz())) {
				Toast.makeText(DataCollect.this, "请选择陈列位置!", Toast.LENGTH_SHORT).show();
				return false;
			}
		}

		if (chkIsPaycust.isChecked()) {
			dataCollectBaseInfo.setIsPaycust("Y");
		} else {
			dataCollectBaseInfo.setIsPaycust("N");
		}

		dataCollectBaseInfo.setDisplayScale(displayScale);
		dataCollectBaseInfo.setVisitNote(txtVisitNote.getText().toString().trim());
		dataCollectBaseInfo.setQuesPlan(txtQuesPlan.getText().toString().trim());
		dataCollectBaseInfo.setVsdate(taskStateStrArr[0]);
		return true;
	}

	private void addListener() {
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				setMenu(tabId);
			}

		});

		btnBack.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				exit();
			}
		});

		btnList.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				openOptionsMenu();
			}
		});

		rgpIsVisit.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (radIsYes.getId() == checkedId) {
					dataCollectBaseInfo.getCustomerInfo().setIsvisit("Y");
				}
				if (radIsNo.getId() == checkedId) {
					dataCollectBaseInfo.getCustomerInfo().setIsvisit("N");
				}
			}
		});
		spiDisplaySite.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				DisplaySiteBean bean = (DisplaySiteBean) parent.getItemAtPosition(position);
				dataCollectBaseInfo.setDisplaySite(bean);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}

		});

		dataCollDayClickView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				final DataCollectDayInfoBean bean = (DataCollectDayInfoBean) parent
						.getItemAtPosition(position);
				final int pos = position;
				AlertDialog.Builder builder = new AlertDialog.Builder(DataCollect.this);
				builder.setTitle("提示信息");
				builder.setMessage("SKU编号  " + bean.getBismt());
				builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						bundle = new Bundle();
						bundle.putSerializable("dataCollectDayInfoList",
								(Serializable) dataCollectDayInfoList);
						bundle.putSerializable("unitInfoList", (Serializable) unitInfoList);
						bundle.putSerializable("dataCollectDayInfo", bean);
						bundle.putInt("dataCollDayPosition", pos);
						intent = new Intent(DataCollect.this, DataCollectDay.class);
						intent.putExtras(bundle);
						DataCollect.this.startActivityForResult(intent, 100001);
					}
				});
				builder.setNeutralButton("取消", null);
				builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dataCollDayAdapter.remove(pos);
						labTextDataCollDaySkuNum.setText(dataCollDayAdapter.getCount() + "");
					}
				});
				builder.show();
			}
		});

		dataCollSaleClickView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				final DataCollectSaleInfoBean bean = (DataCollectSaleInfoBean) parent
						.getItemAtPosition(position);
				final int pos = position;
				AlertDialog.Builder builder = new AlertDialog.Builder(DataCollect.this);
				builder.setTitle("提示信息");
				builder.setMessage("SKU编号  " + bean.getBismt_prom());
				builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						bundle = new Bundle();
						bundle.putSerializable("dataCollectSaleInfoList",
								(Serializable) dataCollectSaleInfoList);
						bundle.putSerializable("unitInfoList", (Serializable) unitInfoList);
						bundle.putSerializable("saleTypeList", (Serializable) saleTypeList);
						bundle.putSerializable("dataCollectSaleInfo", bean);
						bundle.putInt("dataCollSalePosition", pos);
						bundle.putString("vsdate", taskStateStrArr[0]);
						intent = new Intent(DataCollect.this, DataCollectSale.class);
						intent.putExtras(bundle);
						DataCollect.this.startActivityForResult(intent, 100002);
					}
				});
				builder.setNeutralButton("取消", null);
				builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dataCollSaleAdapter.remove(pos);
						labTextDataCollSaleSkuNum.setText(dataCollSaleAdapter.getCount() + "");
					}
				});
				builder.show();
			}
		});
		dataCollCompeteClickView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				final DataCollectCompeteInfoBean bean = (DataCollectCompeteInfoBean) parent
						.getItemAtPosition(position);
				final int pos = position;
				AlertDialog.Builder builder = new AlertDialog.Builder(DataCollect.this);
				builder.setTitle("提示信息");
				builder.setMessage("竞品信息  " + bean.getCompeteBrandInfo().getJpname());
				builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						bundle = new Bundle();
						bundle.putSerializable("competeBrandInfoList",
								(Serializable) competeBrandInfoList);
						bundle.putSerializable("goodsTypeList", (Serializable) goodsTypeList);
						bundle.putSerializable("unitInfoList", (Serializable) unitInfoList);
						bundle.putSerializable("dataCollectCompeteInfo", bean);
						bundle.putInt("dataCollCompetePosition", pos);
						bundle.putString("vsdate", taskStateStrArr[0]);
						intent = new Intent(DataCollect.this, DataCollectCompete.class);
						intent.putExtras(bundle);
						DataCollect.this.startActivityForResult(intent, 100003);
					}
				});
				builder.setNeutralButton("取消", null);
				builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dataCollCompeteAdapter.remove(pos);
						labTextDataCollCompeteSkuNum.setText(dataCollCompeteAdapter.getCount() + "");
					}
				});
				builder.show();
			}
		});
		btnOnPhoto.setOnClickListener(photoListener);

		btnUpPhoto.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				if (null != imageData && imageData.length > 0) {
					httpThread = new HttpThread(handler);
					httpThread.doStart("image");
				} else {
					Toast.makeText(DataCollect.this, "请先拍照,再上传照片!", Toast.LENGTH_SHORT).show();
				}
			}
		});

		spiPictureType.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				PictureTypeBean bean = (PictureTypeBean) parent.getItemAtPosition(position);
				picType = bean.getPictype();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}

		});
	}

	private View.OnClickListener photoListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			try {
				Thread.sleep(1 * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			intent = new Intent(DataCollect.this, CameraPreview.class);
			DataCollect.this.startActivityForResult(intent, 100004);
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
			case 100001 :
				if (resultCode == RESULT_OK) {
					bundle = data.getExtras();
					DataCollectDayInfoBean dataCollectDayInfoBean = (DataCollectDayInfoBean) bundle
							.getSerializable("dataCollectDayInfo");
					int dataCollDayPositionBack = bundle.getInt("dataCollDayPosition", -1);
					if (dataCollDayPositionBack != -1) {
						dataCollDayAdapter.modify(dataCollDayPositionBack, dataCollectDayInfoBean);
					} else {
						dataCollDayAdapter.add(dataCollectDayInfoBean);
						labTextDataCollDaySkuNum.setText(dataCollDayAdapter.getCount() + "");
					}
				}
				break;
			case 100002 :
				if (resultCode == RESULT_OK) {
					bundle = data.getExtras();
					DataCollectSaleInfoBean dataCollectSaleInfoBean = (DataCollectSaleInfoBean) bundle
							.getSerializable("dataCollectSaleInfo");
					int dataCollSalePositionBack = bundle.getInt("dataCollSalePosition", -1);
					if (dataCollSalePositionBack != -1) {
						dataCollSaleAdapter.modify(dataCollSalePositionBack,
								dataCollectSaleInfoBean);
					} else {
						dataCollSaleAdapter.add(dataCollectSaleInfoBean);
						labTextDataCollSaleSkuNum.setText(dataCollSaleAdapter.getCount() + "");
					}
				}
				break;
			case 100003 :
				if (resultCode == RESULT_OK) {
					bundle = data.getExtras();
					DataCollectCompeteInfoBean dataCollectCompeteInfoBean = (DataCollectCompeteInfoBean) bundle
							.getSerializable("dataCollectCompeteInfo");
					int dataCollCompetePositionBack = bundle.getInt("dataCollCompetePosition", -1);
					if (dataCollCompetePositionBack != -1) {
						dataCollCompeteAdapter.modify(dataCollCompetePositionBack,
								dataCollectCompeteInfoBean);
					} else {
						dataCollCompeteAdapter.add(dataCollectCompeteInfoBean);
						labTextDataCollCompeteSkuNum
								.setText(dataCollCompeteAdapter.getCount() + "");
					}
				}
				break;
			case 100004 :
				Bitmap bitmap = null;
				if (resultCode == Activity.RESULT_OK) {

					bundle = data.getExtras();
					byte[] bytes = bundle.getByteArray("data");
					if (null != bytes && bytes.length > 0) {
						bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
					}
					Matrix matrix = new Matrix();
					if (getWindowManager().getDefaultDisplay().getOrientation() == 0) {
						matrix.setRotate(90);
					}
					bitmap = zoomImage(bitmap, 480, 360, matrix);

					ByteArrayOutputStream os = new ByteArrayOutputStream();
					bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
					imageData = os.toByteArray();
					try {
						os.flush();
						os.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					imgPhoto.setImageBitmap(bitmap);
				}
				break;
		}
	}

	private int getSpinnerSelectedItemPosition(DisplaySiteBean displaySite) {
		int position = 0;
		for (int i = 0; i < displaySiteList.size(); i++) {
			if (displaySite.getClwz().equals(displaySiteList.get(i).getClwz())) {
				position = i;
			}
		}
		return position;
	}

	private class HttpThread extends Thread {
		private Handler handle = null;
		private String flag = null;

		public HttpThread(Handler hander) {
			handle = hander;
		}

		public void doStart(String flag) {
			this.flag = flag;
			progressDialog = new ProgressDialog(DataCollect.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("正在提交数据，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.start();
		}

		public void run() {
			Message message = null;
			String result = null;
			String resultImage = null;
			VisitOpeService visitOpeService = new VisitOpeService();
			try {
				if (null != flag && "image".equals(flag)) {
					String imageResult = null;
					try {
						Calendar c = Calendar.getInstance();
						SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSSSSS");
						String time = format.format(c.getTime());
						picName = time + ".jpg";
						imageResult = ImageService.sendImage(picName,
								customerInfoForList.getPartner(), taskStateStrArr[0], loginInfo,
								imageData);
					} catch (Exception e) {
						progressDialog.dismiss();
						bundle = new Bundle();
						message = handle.obtainMessage();
						message.what = CommonStatic.STATE_NORMAL_NULL;
						bundle.putString("flag", flag);
						bundle.putString("resultImage", resultImage);
					}
					if (null != imageResult && "OK".equals(imageResult)) {
						picCommont = txtPictureCommont.getText().toString().trim();
						resultImage = visitOpeService.SetVisitPic(loginInfo, dataCollectBaseInfo,
								taskStateStrArr[0], picType, picName, picCommont);
						if (null != resultImage) {
							progressDialog.dismiss();
							message = handle.obtainMessage();
							bundle = new Bundle();
							message.what = CommonStatic.STATE_NORMAL;
							bundle.putString("resultImage", resultImage);
							bundle.putString("flag", flag);
						} else {
							progressDialog.dismiss();
							bundle = new Bundle();
							message = handle.obtainMessage();
							message.what = CommonStatic.STATE_NORMAL_NULL;
							bundle.putString("flag", flag);
							bundle.putString("resultImage", resultImage);
						}
					} else {
						progressDialog.dismiss();
						message = handle.obtainMessage();
						bundle = new Bundle();
						message.what = CommonStatic.STATE_NORMAL;
						bundle.putString("resultImage", resultImage);
						bundle.putString("flag", flag);
					}
				} else {
					result = visitOpeService.SetVisitInfo(loginInfo, dataCollectBaseInfo,
							dataCollectDayInfoList, dataCollectSaleInfoList,
							dataCollectCompeteInfoList);
					progressDialog.dismiss();
					message = handle.obtainMessage();
					bundle = new Bundle();
					message.what = CommonStatic.STATE_NORMAL;
					bundle.putString("result", result);
				}
				message.setData(bundle);
				handle.sendMessage(message);
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message m) {
			String result = m.getData().getString("result");
			String resultImage = m.getData().getString("resultImage");
			String flag = m.getData().getString("flag");

			switch (m.what) {
				case CommonStatic.STATE_NORMAL :
					if (null != flag && "image".equals(flag)) {
						if ("OK".equals(resultImage)) {
							new AlertDialog.Builder(DataCollect.this).setTitle("提示信息")
									.setMessage("上传图片成功")
									.setNeutralButton("关闭", new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dlg, int sumthin) {
										}
									}).show();
						} else {
							new AlertDialog.Builder(DataCollect.this).setTitle("提示信息")
									.setMessage("上传图片失败,请重新上传!")
									.setNeutralButton("关闭", new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dlg, int sumthin) {
										}
									}).show();
						}
					} else {
						if ("全部提交成功！".equals(result) || "基本信息保存成功".equals(result)) {
							resultStatus = "OK";
						} else {
							resultStatus = "ERROR";
						}

						new AlertDialog.Builder(DataCollect.this).setTitle("提示信息")
								.setMessage(result).setNeutralButton("关闭", null).show();
					}
					break;
				case CommonStatic.STATE_NORMAL_NULL :
					new AlertDialog.Builder(DataCollect.this).setTitle("提示信息")
							.setMessage("程序出现异常,请联系管理员!").setNeutralButton("关闭", null).show();
					break;
			}
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exit();
		}
		return true;
	}

	private void exit() {
		new AlertDialog.Builder(DataCollect.this).setTitle("提示信息").setMessage("确定要退出,数据采集?")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dlg, int sumthin) {
						if ("OK".equals(resultStatus)) {
							if ("Y".equals(dataCollectBaseInfo.getCustomerInfo().getIsvisit())) {
								customerInfoForList.setIsvisit("Y");
							} else {
								customerInfoForList.setIsvisit("N");
							}
							bundle = new Bundle();
							bundle.putSerializable("customerInfoForList", customerInfoForList);
							bundle.putString("resultStatus", resultStatus);
							bundle.putInt("todayVisitPosition", todayVisitPosition);
							intent = new Intent();
							intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							intent.putExtras(bundle);
							DataCollect.this.setResult(RESULT_OK, intent);
							DataCollect.this.finish();
						} else {
							DataCollect.this.finish();
						}
					}
				}).setNeutralButton("取消", null).show();
	}

	private void submitData() {
		if (!getBaseInfo()) {
			return;
		}
		httpThread = new HttpThread(handler);
		httpThread.doStart(null);
	}

	private void setMenu(String tabId) {
		if ("tab5".equals(tabId)) {
			appToolBar.setVisibility(View.GONE);
		} else {
			if (appToolBar.getVisibility() == View.GONE) {
				appToolBar.setVisibility(View.VISIBLE);
			}
			if ("tab1".equals(tabId)) {
				bodyAdapter = new MenuTabContent.MenuBodyAdapter(this, new String[] { "提交数据" },
						new int[] { R.drawable.data_coll_menu_submit }, 13, getResources()
								.getColor(R.color.white));
			} else {
				bodyAdapter = new MenuTabContent.MenuBodyAdapter(this, new String[] { "添加数据",
						"提交数据" },
						new int[] { R.drawable.menu_add, R.drawable.data_coll_menu_submit }, 13,
						getResources().getColor(R.color.white));
			}

			tabMenu = new MenuTabContent(this, new BodyClickEvent(tabId), getResources().getColor(
					R.color.MenuBackGroundColor), R.style.PopupAnimation);

			tabMenu.update();
			tabMenu.SetBodyAdapter(bodyAdapter);
		}
	}

	private class BodyClickEvent implements OnItemClickListener {
		private String tabId = "";

		public BodyClickEvent(String tabId) {
			super();
			this.tabId = tabId;
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			tabMenu.SetBodySelect(position, Color.GRAY);
			switch (position) {
				case 0 :
					if ("tab1".equals(tabId)) {
						submitData();
					} else if ("tab2".equals(tabId)) {
						bundle = new Bundle();
						bundle.putSerializable("dataCollectDayInfoList",
								(Serializable) dataCollectDayInfoList);
						bundle.putSerializable("unitInfoList", (Serializable) unitInfoList);
						intent = new Intent(DataCollect.this, DataCollectDay.class);
						intent.putExtras(bundle);
						DataCollect.this.startActivityForResult(intent, 100001);
					} else if ("tab3".equals(tabId)) {
						bundle = new Bundle();
						bundle.putSerializable("dataCollectSaleInfoList",
								(Serializable) dataCollectSaleInfoList);
						bundle.putSerializable("unitInfoList", (Serializable) unitInfoList);
						bundle.putSerializable("saleTypeList", (Serializable) saleTypeList);
						bundle.putString("vsdate", taskStateStrArr[0]);
						intent = new Intent(DataCollect.this, DataCollectSale.class);
						intent.putExtras(bundle);
						DataCollect.this.startActivityForResult(intent, 100002);
					} else if ("tab4".equals(tabId)) {
						bundle = new Bundle();
						bundle.putSerializable("competeBrandInfoList",
								(Serializable) competeBrandInfoList);
						bundle.putSerializable("goodsTypeList", (Serializable) goodsTypeList);
						bundle.putSerializable("unitInfoList", (Serializable) unitInfoList);
						bundle.putString("vsdate", taskStateStrArr[0]);
						intent = new Intent(DataCollect.this, DataCollectCompete.class);
						intent.putExtras(bundle);
						DataCollect.this.startActivityForResult(intent, 100003);
					}
					break;
				case 1 :
					submitData();
					break;
			}
			if (null != tabMenu) {
				if (tabMenu.isShowing()) {
					tabMenu.dismiss();
				}
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("menu");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		if (tabMenu != null) {
			if (tabMenu.isShowing()) {
				tabMenu.dismiss();
			} else {
				int toolBarHeight = appToolBar.getHeight();
				tabMenu.showAtLocation(findViewById(R.id.appToolBar), Gravity.BOTTOM, 0,
						toolBarHeight);
			}
		}
		return false;
	}

	private Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight, Matrix matrix) {
		int width = bgimage.getWidth();
		int height = bgimage.getHeight();

		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height, matrix, true);
		return bitmap;
	}
}
